package com.example.loginmybatis.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Collection;

/**
 * Copyright © 2021 @author Shu
 * 上海华泉信息科技有限公司
 * https://021hqit.com
 * All rights reserved.
 *
 * @description 用于登录的用户信息，不同平台 + username，组成唯一key
 */
@Data
@NoArgsConstructor
@Entity
@Table(name = "auth_user")
public class AuthUser extends BaseResource implements UserDetails {

    public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();

    private String username;

    @JsonIgnore
    private String password;

    /**
     * 用户在系统中的角色列表，将根据角色对用户操作权限进行限制
     */
    private String roles;

    /**
     * 是否已激活
     */
    private Boolean activated = false;

    /**
     * 账户激活时间
     */
    private Long activateTime;

    /**
     * 用户类型
     */
    private String userType;

    /**
     * 真实姓名
     */
    private String realName;

    private String operator;

    private String brand;

    private String funder;

    @Transient
    private Operator myOperator;

    @Transient
    private Brand myBrand;

    @Transient
    private Funder myFunder;

    @Transient
    private Collection<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
